package com.yj.springbootstudy.mulThread.demo;

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

public class Demo03 {
}


class Person extends Thread{
    @Override
    public void run() {
//        MailBox.createMail();
    }
}

class MailBox {
    private static Map<Integer,Object> mailboxs = new Hashtable<>();

    private static Integer id;

    public synchronized static Integer generatorId(){
        return id++;
    }

    public static void createMail(GuardObject guardObject){
        mailboxs.put(generatorId(),guardObject);
    }

    public static Object getMail(Integer id){
        return mailboxs.remove(id);
    }

    public static Set<Integer> getIds(){
        return mailboxs.keySet();
    }


}

class GuardObject{
    private Object response;


    public Object get() throws InterruptedException {
        synchronized (this){
            while (response == null){
                this.wait();
            }
            return response;
        }
    }


    public  void complete(Object response){
        synchronized (this){
           this.response =response;
           this.notifyAll();
        }
    }
}
